<template>
  <div class="register">
    <canvas id="particleCanvas" class="particle-canvas"></canvas>
    <div class="register-container">
      <div class="register-box">
        <div class="register-logo">
          <span class="logo-text">海隆</span>
          <span class="logo-sub">设备租赁管理平台</span>
        </div>
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username" 
              type="text" 
              auto-complete="off" 
              placeholder="账号"
              class="custom-input"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleRegister"
              class="custom-input"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              auto-complete="off"
              placeholder="确认密码"
              @keyup.enter.native="handleRegister"
              class="custom-input"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="registerForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleRegister"
              class="custom-input"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="register-code">
              <img :src="codeUrl" @click="getCode" class="register-code-img"/>
            </div>
          </el-form-item>
          <el-form-item class="register-button-container">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="register-button"
              @click.native.prevent="handleRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
          </el-form-item>
          <div class="login-link">
            <span>已有账号?</span>
            <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
          </div>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 hello.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true,
      particles: [],
      animationFrame: null
    };
  },
  created() {
    this.getCode();
  },
  mounted() {
    this.initParticles();
    window.addEventListener('resize', this.resizeCanvas);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas);
    if (this.animationFrame) {
      cancelAnimationFrame(this.animationFrame);
    }
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    },
    initParticles() {
      const canvas = document.getElementById('particleCanvas');
      const ctx = canvas.getContext('2d');
      
      const resizeCanvas = () => {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
      };
      
      resizeCanvas();
      this.resizeCanvas = resizeCanvas;
      
      // 创建粒子
      const particleCount = 100;
      
      for (let i = 0; i < particleCount; i++) {
        this.particles.push({
          x: Math.random() * canvas.width,
          y: Math.random() * canvas.height,
          radius: Math.random() * 3 + 1,
          color: `rgba(${Math.floor(Math.random() * 100) + 100}, ${Math.floor(Math.random() * 100) + 100}, 255, ${Math.random() * 0.5 + 0.3})`,
          speedX: Math.random() * 2 - 1,
          speedY: Math.random() * 2 - 1,
          connections: []
        });
      }
      
      const draw = () => {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        // 更新和绘制粒子
        for (let i = 0; i < this.particles.length; i++) {
          const p = this.particles[i];
          
          // 移动粒子
          p.x += p.speedX;
          p.y += p.speedY;
          
          // 边界检查
          if (p.x < 0 || p.x > canvas.width) p.speedX *= -1;
          if (p.y < 0 || p.y > canvas.height) p.speedY *= -1;
          
          // 绘制粒子
          ctx.beginPath();
          ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
          ctx.fillStyle = p.color;
          ctx.fill();
          
          // 寻找附近的粒子并连线
          p.connections = [];
          for (let j = i + 1; j < this.particles.length; j++) {
            const p2 = this.particles[j];
            const distance = Math.sqrt(Math.pow(p.x - p2.x, 2) + Math.pow(p.y - p2.y, 2));
            
            if (distance < 100) {
              p.connections.push(j);
              ctx.beginPath();
              ctx.moveTo(p.x, p.y);
              ctx.lineTo(p2.x, p2.y);
              ctx.strokeStyle = `rgba(30, 144, 255, ${0.2 * (1 - distance / 100)})`;
              ctx.lineWidth = 0.5;
              ctx.stroke();
            }
          }
        }
        
        this.animationFrame = requestAnimationFrame(draw);
      };
      
      draw();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: #070b20;
  background-image: linear-gradient(135deg, rgba(0, 0, 0, 0.9) 0%, rgba(20, 20, 35, 0.9) 100%);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(30, 144, 255, 0.05) 0%, transparent 80%);
    animation: pulse 15s infinite;
  }
}

.particle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 0.3;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 0.3;
  }
}

.register-container {
  z-index: 10;
  width: 100%;
  max-width: 1200px;
  padding: 0 20px;
  position: relative;
  display: flex;
  justify-content: center;
}

.register-box {
  width: 480px;
  backdrop-filter: blur(20px);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5),
              0 0 40px rgba(30, 144, 255, 0.15),
              0 0 0 1px rgba(30, 144, 255, 0.1);
  background: rgba(15, 23, 42, 0.7);
  transform: perspective(1000px) translateZ(0);
  transition: all 0.5s;
  position: relative;
  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(30, 144, 255, 0.6), transparent);
    opacity: 0.5;
  }
  
  &:after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(30, 144, 255, 0.6), transparent);
    opacity: 0.2;
  }
}

.register-logo {
  text-align: center;
  margin: 40px 0 30px;
  
  .logo-text {
    font-size: 52px;
    font-weight: bold;
    color: #1E90FF;
    text-shadow: 0 0 20px rgba(30, 144, 255, 0.9);
    letter-spacing: 2px;
    position: relative;
    display: inline-block;
    
    &:after {
      content: '';
      position: absolute;
      width: 100%;
      height: 6px;
      bottom: -8px;
      left: 0;
      background: linear-gradient(90deg, transparent, #1E90FF, transparent);
      border-radius: 50%;
      filter: blur(5px);
    }
  }
  
  .logo-sub {
    font-size: 28px;
    color: rgba(255, 255, 255, 0.9);
    margin-left: 8px;
    font-weight: 300;
    letter-spacing: 2px;
  }
}

.register-form {
  width: 85%;
  margin: 0 auto;
  padding-bottom: 40px;
  
  .el-form-item {
    margin-bottom: 28px;
  }
}

.custom-input {
  height: 50px;
  
  .el-input__inner {
    height: 50px;
    line-height: 50px;
    background: rgba(10, 15, 30, 0.6) !important;
    border: 1px solid rgba(30, 144, 255, 0.2) !important;
    border-radius: 8px;
    color: #eee;
    font-size: 15px;
    padding-left: 50px !important;
    transition: all 0.3s;
    
    &::placeholder {
      color: rgba(255, 255, 255, 0.4);
    }
    
    &:focus {
      border: 1px solid rgba(30, 144, 255, 0.8) !important;
      box-shadow: 0 0 15px rgba(30, 144, 255, 0.3) !important;
      background: rgba(15, 20, 40, 0.8) !important;
    }
  }
  
  .el-input__prefix {
    left: 15px;
    top: 0;
    height: 100%;
    display: flex;
    align-items: center;
  }
  
  .input-icon {
    height: 20px;
    width: 20px;
    color: #1E90FF;
    z-index: 2;
  }
}

.register-button-container {
  margin-bottom: 15px !important;
}

.register-button {
  width: 100%;
  background: linear-gradient(90deg, #1E90FF 0%, #4169E1 100%);
  border: none;
  height: 50px;
  border-radius: 8px;
  font-size: 17px;
  letter-spacing: 2px;
  position: relative;
  overflow: hidden;
  transition: all 0.4s;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    animation: buttonShine 3s infinite;
  }
  
  &:hover, &:focus {
    transform: translateY(-2px);
    background: linear-gradient(90deg, #4169E1 0%, #1E90FF 100%);
    opacity: 0.95;
    box-shadow: 0 7px 25px rgba(30, 144, 255, 0.35), 0 0 15px rgba(30, 144, 255, 0.2);
  }
  
  &:active {
    transform: translateY(0);
  }
}

.login-link {
  text-align: center;
  margin-top: 10px;
  color: rgba(255, 255, 255, 0.5);
  font-size: 14px;
  
  .link-type {
    color: #1E90FF;
    text-decoration: none;
    margin-left: 5px;
    position: relative;
    transition: all 0.3s;
    font-weight: 500;
    
    &:after {
      content: '';
      position: absolute;
      width: 0;
      height: 1px;
      bottom: -2px;
      left: 0;
      background-color: #1E90FF;
      transition: width 0.3s;
    }
    
    &:hover {
      text-shadow: 0 0 8px rgba(30, 144, 255, 0.7);
      
      &:after {
        width: 100%;
      }
    }
  }
}

@keyframes buttonShine {
  0% {
    left: -100%;
  }
  20% {
    left: 100%;
  }
  100% {
    left: 100%;
  }
}

.register-code {
  width: 33%;
  height: 50px;
  float: right;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 8px;
    height: 48px;
    border: 1px solid rgba(30, 144, 255, 0.3);
    filter: brightness(0.9);
    transition: all 0.3s;
    
    &:hover {
      filter: brightness(1.1);
      box-shadow: 0 0 10px rgba(30, 144, 255, 0.5);
    }
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.3);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 10;
}

.el-form-item__content {
  position: relative;
  z-index: 1;
}

@media (max-width: 768px) {
  .register-box {
    width: 100%;
    max-width: 400px;
  }
  
  .register-logo .logo-text {
    font-size: 44px;
  }
  
  .register-logo .logo-sub {
    font-size: 24px;
  }
}
</style>
